
package com.lht.admin.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lht.admin.annotation.SysLog;
import com.lht.admin.constants.DataValidConstant;
import com.lht.admin.entity.ApplyInfoEntity;
import com.lht.admin.entity.SysUserEntity;
import com.lht.admin.service.ApplyInfoService;
import com.lht.admin.service.PersonInfoService;
import com.lht.admin.utils.R;
import com.lht.admin.vo.UpdatePromoUserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 借款数据信息表Controller
 *
 * @author ZhengZhanWu
 * @date 2019-12-29 13:30:03
 */
@Slf4j
@RestController
@RequestMapping("/applyinfo")
public class ApplyInfoController extends AbstractController {

    @Autowired
    private ApplyInfoService applyInfoService;
    @Resource
    private PersonInfoService personInfoService;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return R
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("applyinfo:list")
    public R queryAll(@RequestParam Map<String, Object> params) {
        params.put("isDelete", DataValidConstant.IS_DELETE_FALSE_STR);
        List<ApplyInfoEntity> list = applyInfoService.queryAll(params);

        return R.ok().put("list", list);
    }

    /**
     * 分页查询借款数据信息表
     *
     * @param params 查询参数
     * @return R
     */
    @GetMapping("/list")
    @RequiresPermissions("applyinfo:list")
    public R list(@RequestParam Map<String, Object> params) {
        params.put("isDelete", DataValidConstant.IS_DELETE_FALSE_STR);
        Page page = applyInfoService.queryPage(params);

        return R.ok().put("page", parseToPageUtils(page));
    }

    /**
     * 根据主键查询详情
     *
     * @param id 主键
     * @return R
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("applyinfo:info")
    public R info(@PathVariable("id") Long id) {
        ApplyInfoEntity applyInfo = applyInfoService.queryById(id);

        return R.ok().put("applyInfo", applyInfo);
    }

    /**
     * 新增借款数据信息表
     *
     * @param applyInfo applyInfo
     * @return R
     */
    @SysLog("新增借款数据信息表")
    @RequestMapping("/save")
    @RequiresPermissions("applyinfo:save")
    public R save(@RequestBody ApplyInfoEntity applyInfo) {
        SysUserEntity operator = getUser();

        log.info("Function[save] operator:{},info:{}", null != operator ? operator.getUserId() : "", JSON.toJSONString(applyInfo));

        applyInfoService.add(operator, applyInfo);

        return R.ok();
    }

    /**
     * 修改借款数据信息表
     *
     * @param applyInfo applyInfo
     * @return R
     */
    @SysLog("修改借款数据信息表")
    @RequestMapping("/update")
    @RequiresPermissions("applyinfo:update")
    public R update(@RequestBody ApplyInfoEntity applyInfo) {

        applyInfoService.update(applyInfo);

        return R.ok();
    }

    /**
     * 根据主键删除借款数据信息表
     *
     * @param ids ids
     * @return R
     */
    @SysLog("删除借款数据信息表")
    @RequestMapping("/delete")
    @RequiresPermissions("applyinfo:delete")
    public R delete(@RequestBody Long[] ids) {
        applyInfoService.deleteBatch(ids);

        return R.ok();
    }


    /**
     * 更新经纪人数据
     *
     * @return R
     */
    @RequiresPermissions("applyinfo:update")
    @RequestMapping("/updatePromoUser")
    public R updatePromoUser(String info) {
        log.info("Function[updatePromoUser] update {}", info);

        UpdatePromoUserVO promoUserVO = JSON.parseObject(info, UpdatePromoUserVO.class);

        applyInfoService.updatePromoUser(getUserId(), promoUserVO.getIds(), promoUserVO.getPromoUserId());

        return R.ok();
    }


    /**
     * 检查身份证号码是否已经存在
     *
     * @param idCardNo 身份证号码
     * @return R
     */
    @RequestMapping("/isExists")
    public R isExists(String idCardNo) {
        log.info("Function[isExistsInfo] idCardNo:{}", idCardNo);

        boolean exitsInfo = personInfoService.isExistsInfo(idCardNo);

        log.info("Function[isExists] idCard:{} exists:{}", idCardNo, exitsInfo);


        return R.ok().put("exists", exitsInfo);
    }
}
